package com.pwp.activity;

import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;

import com.agent.crm.base.BaseActivity;
import com.agent.crm.sechdule.receiver.RepeatingAlarm;
import com.agent.crm.view.R;
import com.pwp.dao.ScheduleDAO;

public class StopScheduleByNotificationActivity extends BaseActivity {

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	protected int initLayout() {
		// TODO Auto-generated method stub
		return R.layout.stop_schedule_by_notification;
	}

	@Override
	protected View initCustomLayout() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void initViewCmp() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("是否停止该任务")
				.setPositiveButton("确定", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Intent i=getIntent();
//						ScheduleVO scheduleVO = (ScheduleVO)i
//								.getSerializableExtra("schedule");
						int id= i.getIntExtra("scheduleId",1000);
						// 停止任务提示
						Intent intent = new Intent(
								StopScheduleByNotificationActivity.this,
								RepeatingAlarm.class);
						PendingIntent sender = PendingIntent.getBroadcast(
								StopScheduleByNotificationActivity.this,
								id, intent, 0);
						// And cancel the alarm.
						AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
						am.cancel(sender);
						ScheduleDAO dao =new ScheduleDAO(StopScheduleByNotificationActivity.this);
						dao.resetScheduleStatusFlagsToFinished(id);
						dao.destoryDB();
						NotificationManager nm =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
						nm.cancel(id);
						StopScheduleByNotificationActivity.this.finish();
					}
				}).setNegativeButton("取消", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						StopScheduleByNotificationActivity.this.finish();
					}
				}).setOnKeyListener(new DialogInterface.OnKeyListener() {
					@Override
					public boolean onKey(DialogInterface dialog, int keyCode,
							KeyEvent event) {
						if (keyCode == KeyEvent.KEYCODE_BACK) {
							return true;
						}
						return false;
					}
				}).show();
	}

	@Override
	protected void initAsycnData() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void changeDefaultSetting() {
		// TODO Auto-generated method stub

	}
	
}
